;; legate.el


;; [[file:~/literate-programming/soul-hunter-awakening-damage-calculator-web-edition.org::*legate.el][legate.el:1]]
(defun legate::switch-format (string where)
  "转换类似格式的数据

`where'∈{body,argument,sum}
如果是＂body＂，即作为函数主体来添加：
  ＂武器基础暴击率:0.22,
武器精铸暴击率:0.05＂
  =>
  ＂let 武器基础暴击率=object.武器基础暴击率
let 武器精铸暴击率=object.武器精铸暴击率＂

如果是＂argument＂，即作为函数变量来添加：
    ＂武器基础暴击率:0.22,
武器精铸暴击率:0.05＂
  =>
  ＂武器基础暴击率:object.武器基础暴击率,
武器精铸暴击率:object.武器精铸暴击率＂

如果是＂sum＂，即以相加的方式来添加：
  ＂武器基础暴击率:0.22,
武器精铸暴击率:0.05＂
  =>
  ＂武器基础暴击率+
  武器精铸暴击率＂"
  (let ((list
         (mapcar (lambda (item)
                   (car (split-string item ":" t)))
                 (split-string string nil t))))
    (cl-case where
      (body
       (mapconcat (lambda (item)
                    (format "let %s=object.%s" item item))
                  list
                  "\n"))
      (argument
       (mapconcat (lambda (item)
                    (format "%s:object.%s" item item))
                  list
                  ",\n"))
      (sum
       (mapconcat (function identity)
                  list
                  "+\n")))))

(defun legate:switch-format ()
  (interactive)
  (let ((string (read-string "String to switch format: "))
        (where (intern
                (completing-read
                 "Where to insert: "
                 (quote
                  (body argument sum))
                 nil t))))
    (let ((switched (legate::switch-format string where)))
      (message (format "%s" switched))
      (when switched
        (kill-new switched)))))
;; legate.el:1 ends here
